<?php
namespace app\html5\controller;
use app\Db2;
use think\facade\Db;
use app\H5AuthController;

class Zhenduan extends H5AuthController{
    public function get_zhenduan_list(){
        $user = $this->user;// 获取当前用户信息
        // print_r($user);
        $curpage = input('curpage');
        $listnum = input('listnum');

        $curpage = $curpage?$curpage:0;
        $listnum = $listnum?$listnum:20;

        $where = [];
        if($user['type']==1){
            // 前台医生可以查看，医生提交的已付款，没有分配后台医生的

            $where = [
                [
                    'status'=>1,
                    'step'=>1,
                    'type'=>1,
                    'docid'=>$user['id'],
                ],
            ];
        }elseif($user['type']==2){
            // 后台医生可以查看，分配给自己的，自己没有处理的

            $where = [
                [
                    'status'=>1,
                    'ht_doc_id'=>$user['id'],
                ],
                '(`type`=0 and `step`=1) or (`type`=1 and `step`=3)'
            ];
        }elseif($user['type']==5){
            // 管理员可以查看，自己提交的，未付款或者医生提交的已付款
            $where = [
                [
                    'status'=>1,
                ],
                '(`type`=0 and `step`=1) or (`type`=1 and `step`=2)'
            ];
        }else{
            return;
        }


        $list = Db2::get_datalist_page("zhenduanjilu",$where,$curpage,$listnum,null,'id desc');
//        $sql = 'select * from zhenduanjilu where `status`=1 and ((`type`=0 and `step`=1) or (`type`=1 and `step`=2))';

//        $list = Db::query($sql);

//        $userid_arr = array_column($list,'userid');
//
//        $users = Db2::get_datalist("user",[['id' =>['in',implode(',',$userid_arr)]]]);
//
//        $user = array_column($users,NULL,'id');
//
//        foreach ($list as &$v){
//            $v['userinfo'] = $user[$v['userid']];
//        }
//        print_r($list);
//        die();
//        $data = array_to_tree($list);
        return rejson(200, '操作成功', $list);
    }

    public function get_zhenduan_info()
    {
        $user = $this->user;// 获取当前用户信息
        $id = input('id');

        $data = Db2::get_data("zhenduanjilu",[['id' =>$id]]);

        $users = Db2::get_data("user",[['id' =>$data['userid']]]);
        $data['user'] = $users;
        return rejson(200, '操作成功', $data);
    }
    //前台医生提交处理结果
    public function doc1_save()
    {
        $id = input('id');
        $text = input('text');

        if(!$text || $text == 'undefined') return rejson(302,'必填字段确实');
        
        $user = $this->user;// 获取当前用户信息
        $doc_id = $user['id'];

        $res = Db2::update_data("zhenduanjilu",[['id'=>$id]],['step'=>2,'text2'=>$text,'docid'=>$doc_id]);
        
        return $res?rejson(200, '操作成功'):rejson(302, '操作失败，请刷新后再试');
    }

    public function get_doc_tree()
    {
        $doc_list = Db2::get_datalist("user_doc",[['type'=>2]]);

        $dep_list = Db2::get_datalist("dep_table");

        $dep_list = array_column($dep_list,NULL,'id');

        foreach ($doc_list as &$doc){
            $doc['dep_name'] = $dep_list[$doc['depid']]['name'];
        }

        return rejson(200, '操作成功', $doc_list);
    }

    public function fenpei_doc()
    {
        $zhenduan_id = input('zhenduan_id');
        $doc_id = input('doc_id');

        if(!$zhenduan_id || $zhenduan_id == 'undefined') return rejson(302,'必填字段确实');
        if(!$doc_id || $doc_id == 'undefined') return rejson(302,'必填字段确实');
        
        $user = $this->user;// 获取当前用户信息

        $res = Db2::update_data("zhenduanjilu",[['id'=>$zhenduan_id]],['step'=>3,'ht_doc_id'=>$doc_id,'admin_doc_id'=>$user['id']]);

        return $res?rejson(200, '操作成功'):rejson(302, '操作失败，请刷新后再试');
    }
    //后台医生提交处理结果
    public function doc2_save()
    {
        $id = input('id');
        $text = input('text');

        if(!$text || $text == 'undefined') return rejson(302,'必填字段确实');
        
        $user = $this->user;// 获取当前用户信息
        $doc_id = $user['id'];

        $res = Db2::update_data("zhenduanjilu",[['id'=>$id]],['step'=>3,'text3'=>$text,'docid'=>$doc_id]);
        
        return $res?rejson(200, '操作成功'):rejson(302, '操作失败，请刷新后再试');
    }
}

